package com.ldc.api.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ldc.api.common.ParamsHolder;
import com.ldc.common.state.StateCode;
import com.ldc.common.utils.CollectionUtils;
import com.ldc.model.SysUser;
import com.ldc.model.UserInfo;
import com.ldc.model.base.ApiBean;
import com.ldc.model.params.PageParams;
import com.ldc.service.service.UserInfoService;
import com.ldc.service.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 功能描述
 *
 * @author liudi
 * @since 2023/7/16
 */
@RestController
@RequestMapping("/admin")
public class UserManagerController {
    @Autowired
    UserService userService;
    @Autowired
    PasswordEncoder passwordEncoder;
    @Autowired
    UserInfoService userInfoService;

    /**
     * 添加新用户
     *
     * @param params
     * @return
     */
    @PostMapping("/user/new")
    public ApiBean<?> userNew(@RequestBody SysUser params) {
        final SysUser hasData = userService.findByUserName(params.getUserName());
        if (hasData == null) {
            SysUser userBean = ParamsHolder.createNewUser(params.getUserName(), passwordEncoder.encode(params.getUserName()));
            boolean success = userService.save(userBean);
            if (success) {
                userBean = userService.findByUserName(params.getUserName());
                UserInfo userInfo = ParamsHolder.createNewUserInfo(userBean);
                userInfoService.save(userInfo);
            }
            return ApiBean.success(success);
        }
        return ApiBean.result(StateCode.USER_ALREADY_EXISTS);
    }

    /**
     * 删除用户
     *
     * @param ids
     * @return
     */
    @PostMapping("/user/delete")
    public ApiBean<?> userDelete(@RequestBody List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return ApiBean.success(false);
        }
        final List<Long> uids = userService.getUserIds(ids);
        boolean success = false;
        if (CollectionUtils.isNotEmpty(uids)) {
            int code = userService.batchDeleteByIds(uids);
            success = code > 1;
            if (success) {
                userInfoService.batchDeleteByUIds(uids);
            }
        }
        return ApiBean.success(success);
    }

    /**
     * 用户列表
     *
     * @param params
     * @return
     */
    @PostMapping("/user/list")
    public ApiBean<?> userList(@RequestBody PageParams params) {
        IPage<SysUser> record = userService.findPageByPageQuery(params.getPage(), params.getOffset(), params.getQuery());
        if (record == null) {
            return ApiBean.failed("数据为空");
        }
        return ApiBean.successPage(record);
    }
}
